package com.zhu.algorithm.jzOffer.queueAndStack;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2023/01/15/15:33
 * @Description:
 */
public class Solution30 {

    private int[] element = new int[10];
    private int elementCount = 0;
    private int Capacity = 10;

    private int getMinElement() {
        if (elementCount == 0) {
            return 0;
        }
        int minElement = element[0];
        for (int i = 0; i < elementCount; i++) {
            if (minElement > element[i]) {
                minElement = element[i];
            }
        }
        return minElement;
    }

    public void push(int node) {
        if (element.length == elementCount) {
            System.out.println("--grow()--");
            grow();
        }
        element[elementCount++] = node;
    }

    private void grow() {
        int newCapacity = Capacity + (Capacity >> 1);
        element = Arrays.copyOf(element, newCapacity);
        Capacity = newCapacity;
    }

    public void pop() {
        if (elementCount > 0) {
            elementCount--;
        }
    }

    public int top() {
        if (elementCount == 0) {
            return 0;
        }
        return element[elementCount-1];
    }

    public int min() {
        return getMinElement();
    }

    // 识别操作类型，取出操作值
    private void handle(String operation) {
        String operate = operation.substring(0, 3);
        System.out.println(operate);
        if ("PSH".equals(operate)) {
            int node = Integer.parseInt(operation.substring(3));
            push(node);
        } else if ("POP".equals(operate)) {
            pop();
        } else if ("MIN".equals(operate)) {

            System.out.println(min());
        } else if ("TOP".equals(operate)) {

            System.out.println(top());
        }
    }
    public static void main(String[] args) {
        String[] operations = {"PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","PSH-1","PSH2","MIN","TOP","POP","PSH1","TOP","MIN"};
        Solution30 solution30 = new Solution30();
        // 识别操作类型，取出操作值
        for (String operation : operations) {
            solution30.handle(operation);
        }
        System.out.println(operations);
    }
}
